package gz.metro.datasync.utils.page;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：张任豪
 * @date ：2022/9/30 11:14
 * @description：
 */
public class PageQueryRequest {

    protected Integer rows;

    protected Integer page;

    protected String sidx;

    protected String sord;

    protected String sortd = "";

    protected Map<String, Object> parameter = new HashMap<String,Object>();

    protected String filters;

    protected Filters params;

    public PageQueryRequest() {
    }


    protected String renameKey(String fieldParamKey) {
        String newKey = fieldParamKey;

        for(int i = 1; this.parameter.containsKey(newKey); ++i) {
            newKey = fieldParamKey + i;
        }

        return newKey;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }

    public Filters getParams() {
        return JSONObject.parseObject(this.filters,Filters.class);
    }

    public void setParams(Filters params) {
        this.params = params;
    }

    public void setParameter(String key, Object value) {
        this.parameter.put(key, value);
    }

    public Integer getSize() {
        if (this.rows == null) {
            this.rows = 10;
        }

        return this.rows;
    }

    public void setSize(Integer size) {
        this.rows = size;
    }

    public Integer getIndex() {
        if (this.page == null) {
            this.page = 1;
        }

        return this.page;
    }

    public void setIndex(Integer index) {
        this.page = index;
    }

    public Map<String, Object> getParameter() {
        return this.parameter;
    }

    public void setParameter(Map<String, Object> parameter) {
        this.parameter = parameter;
    }

    public String getSort() {
        if (StringUtils.isNotBlank(this.sidx)) {
            this.sortd = this.sidx + " " + this.sord;
            return this.sortd;
        } else {
            return "";
        }
    }

    public static class Rules {
        private String field;
        private String op;
        private String type;
        private String data;

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getOp() {
            return op;
        }

        public void setOp(String op) {
            this.op = op;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    public static class Filters {
        private String groupOp;
        private List<Rules> rules;

        public String getGroupOp() {
            return groupOp;
        }

        public void setGroupOp(String groupOp) {
            this.groupOp = groupOp;
        }

        public List<Rules> getRules() {
            return rules;
        }

        public void setRules(List<Rules> rules) {
            this.rules = rules;
        }
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    public String getSord() {
        return sord;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public String getSortd() {
        return sortd;
    }

    public void setSortd(String sortd) {
        this.sortd = sortd;
    }
}